package org.itboys.weixin.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.commons.utils.collection.CommonCollectionUtils;
import org.itboys.commons.utils.collection.FetchCondition;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.weixin.entity.WeixinConfig;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;

/**
 * 微信后台
 * @author wzp
 *
 */
@Service
public class WeixinConfigService extends BaseWeixinService<WeixinConfig, Long> {

	private static final long serialVersionUID = -4265593447686455571L;
	
	@Resource(name="weixinDS")
	private MongoDataSource weixinDetailDataSource;

	@Override
	protected MongoDataSource getMongoDataSource() {
		return weixinDetailDataSource;
	}

	@Override
	protected Class<WeixinConfig> getEntityClass() {
		return WeixinConfig.class;
	}
	
	public void save(WeixinConfig detail){
		detail.setCreateTime(new Date());
		detail.setCreator(AdminSessionHolder.getAdminUserId());
		super.save(detail);
	}
	
	public void update(WeixinConfig detail){
		WeixinConfig detail2 = getById(detail.getId());
		detail.setCreateTime(detail2.getCreateTime());
		detail.setCreator(detail2.getCreator());
		detail.setUpdater(AdminSessionHolder.getAdminUserId());
		detail.setUpdateTime(new Date());
		if(StringUtils.isNotEmpty(detail.getLogo())){
			detail.setLogo(detail2.getLogo());
		}
		if(StringUtils.isNotEmpty(detail.getEwm())){
			detail.setEwm(detail2.getEwm());
		}
		if(StringUtils.isNotEmpty(detail.getEwm1())){
			detail.setEwm1(detail2.getEwm1());
		}
		if(StringUtils.isNotEmpty(detail.getEwm2())){
			detail.setEwm2(detail2.getEwm2());
		}
		if(StringUtils.isNotEmpty(detail.getEwm3())){
			detail.setEwm3(detail2.getEwm3());
		}
		if(StringUtils.isNotEmpty(detail.getEwm4())){
			detail.setEwm4(detail2.getEwm4());
		}
		if(StringUtils.isNotEmpty(detail.getEwm5())){
			detail.setEwm5(detail2.getEwm5());
		}
		super.update(detail);
	}
	
	public  List<WeixinConfig>  getByParentIdAndProjectId(Long parentId){
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(1);
		param.put("parentId", parentId);
		return super.list(param);
	}
	
	///////  getRootsItem  获取一级栏目 用户关联微信
	public List<WeixinConfig> getRootsItem2(List<WeixinConfig> all){
		List<WeixinConfig> list = CommonCollectionUtils.filterCollection(all, new FetchCondition<WeixinConfig>() {
			@Override
			public boolean needFetch(WeixinConfig w) {
				return w.getParentId()==null || w.getParentId().equals(0L);
			}
		});
		return list;
	}
	/**
	 * 获取一级栏目
	 * 
	 * @param item
	 * @return
	 */
	public List<WeixinConfig> getRootsItem(List<WeixinConfig> item) {
		return CommonCollectionUtils.filterCollection(item,
				new FetchCondition<WeixinConfig>() {
					@Override
					public boolean needFetch(WeixinConfig t) {
						return t.getParentId().equals(0L);
					}
				});
	}

	/*public List<WeixinConfig> findByCodes(List<String> code) {
		return weixinConfigMapper.findByCodes(code);
	}*/
	
	/**
	 * 获取全部菜单
	 * @return
	 */
	public List<WeixinConfig> getAllWeixinConfig(Long id){
		Map<String,Object> sqlMap = Maps.newHashMapWithExpectedSize(1);
		sqlMap.put("id", id);
		return super.list(sqlMap);
	}
	//根据id加载菜单项
	public List<WeixinConfig> getChilidsByParentid(Long id){
		Map<String,Object> queryParam = Maps.newHashMapWithExpectedSize(1);
		queryParam.put("id", id);
		List<WeixinConfig> list = super.list(queryParam);
		return list;
	}
	///获取某个实例的所有子类列表
	
	public List<WeixinConfig> getChildList(Long pId,List<WeixinConfig> allList){
		if(pId != null && allList != null){
			List<WeixinConfig> childList = new ArrayList<WeixinConfig>();//这里如何设置初始大小?
			for(WeixinConfig weixinConfig : allList){
				if(pId == weixinConfig.getParentId()){
					childList.add(weixinConfig);
				}
			}
			return childList;
		}
		return null;
	}

}
